/* eslint-disable multiline-ternary */
// A wrapper for <Route> that redirects to the login

import { hasToken } from '@/utils/storage'
import { Redirect, Route, RouteProps } from 'react-router-dom'

// screen if you're not yet authenticated.
export default function PrivateRoute ({ children, ...rest }: RouteProps) {
  return (
    <Route
      {...rest}
      render={({ location }) =>
        hasToken() ? (
          children
        ) : (
          <Redirect
            to={{
              pathname: '/login',
              state: { from: location.pathname }
            }}
          />
        )}
    />
  )
}
